package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dell
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2022-05-18 09:00:54
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueMapper attrValueMapper;

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseMapper.getAttrInfoList(category1Id, category2Id, category3Id);
    }

    @Transactional
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        baseMapper.insert(baseAttrInfo);
        //获取提交过来的所有属性
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue value : attrValueList) {
            value.setAttrId(baseAttrInfo.getId());
            attrValueMapper.insert(value);
        }
    }

    @Transactional
    @Override
    public void saveOrUpdateAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() != null) {
            //修改
            updateAttrInfo(baseAttrInfo);
        } else {
            //添加
            saveAttrInfo(baseAttrInfo);
        }
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id", attrId);
        return attrValueMapper.selectList(queryWrapper);
    }

    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
        baseMapper.updateById(baseAttrInfo);
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        //存储传入的属性 ID
        List<Long> list = new ArrayList<>();
        for (BaseAttrValue value : attrValueList) {
            if (value.getId() != null) {

                //修改属性
                attrValueMapper.updateById(value);
            } else {
                //添加属性
                value.setAttrId(baseAttrInfo.getId());
                attrValueMapper.insert(value);
            }
            list.add(value.getId());
        }
        //如果传入的Id大于0
        if (list.size() > 0) {
            QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_id", baseAttrInfo.getId());
            queryWrapper.notIn("id", list);
            attrValueMapper.delete(queryWrapper);
        } else {
            //如果传入的ID为空，则说明全删完
            QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_id", baseAttrInfo.getId());
            attrValueMapper.delete(queryWrapper);
        }

    }
}
